package com.jay.compass;

import com.jay.compass.utils.BTimer;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.provider.SyncStateContract.Constants;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

@SuppressLint("NewApi")
public class FragmentTool extends Fragment implements View.OnClickListener {
	private Button compassButton;
	private Button locationButton;
	private Button shareButton;
	private FragmentCallbackListener mListener;
	private IWXAPI wxApi;
	private String WX_APP_ID = "wxe17a3bf4f89e0beb";

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		System.out.println("onAttach");
		mListener = (FragmentCallbackListener) activity;
		wxApi = WXAPIFactory.createWXAPI(getActivity(), WX_APP_ID, true);
		wxApi.registerApp(WX_APP_ID);
	}

	@Override
	public void onDetach() {
		// TODO Auto-generated method stub
		super.onDetach();
		System.out.println("onDetach");
	}

	public void onSecondPast(int time) {
		if (time > 0) {
			locationButton.setEnabled(false);
			locationButton.setText("北斗定位(" + time + "s)");
		} else if (time == 0) {
			locationButton.setEnabled(true);
			locationButton.setText("北斗定位");
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.fragment_tool, container,
				false);
		compassButton = (Button) rootView
				.findViewById(R.id.tool_compass_button);
		locationButton = (Button) rootView
				.findViewById(R.id.tool_location_button);
		shareButton = (Button) rootView.findViewById(R.id.tool_share_button);

		compassButton.setOnClickListener(this);
		locationButton.setOnClickListener(this);
		shareButton.setOnClickListener(this);
		if (BTimer.getInstance().isRunning()) {
			int time = BTimer.getInstance().gettimeleft();
			locationButton.setEnabled(false);
			locationButton.setText("北斗定位(" + time + "s)");
		}
		return rootView;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		System.out.println("onclick!");

		if (v.getId() == R.id.tool_compass_button) {
			Intent intent = new Intent(getActivity(), CalibrateActivity.class);
			startActivity(intent);
		} else if (v.getId() == R.id.tool_location_button) {
			mListener.doLocate();
		} else if (v.getId() == R.id.tool_share_button) {
			System.out.println("share!");
			AlertDialog isExit = new AlertDialog.Builder(getActivity()).create();
			isExit.setTitle("提示");
			isExit.setMessage("分享到？");
			isExit.setButton("好友", shareListener);
			isExit.setButton2("朋友圈", shareListener);
			isExit.show();
		}
	}
	
	DialogInterface.OnClickListener shareListener = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			// TODO Auto-generated method stub
			if (which == AlertDialog.BUTTON_POSITIVE) {
				wechatShare(0);
			} else if (which == AlertDialog.BUTTON_NEGATIVE) {
				wechatShare(1);
			}
		}
	};

	private void wechatShare(int flag) {
		WXWebpageObject webpage = new WXWebpageObject();
		webpage.webpageUrl = "http://www.china-compass.com";
		WXMediaMessage msg = new WXMediaMessage(webpage);
		msg.title = "海员通";
		msg.description = "使用海员通实时跟家人保持联系";
		Bitmap thumb = BitmapFactory.decodeResource(getResources(),
				R.drawable.app_logo);
		msg.setThumbImage(thumb);

		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = String.valueOf(System.currentTimeMillis());
		req.message = msg;
		req.scene = flag == 0 ? SendMessageToWX.Req.WXSceneSession
				: SendMessageToWX.Req.WXSceneTimeline;
		System.out.println("send");
		wxApi.sendReq(req);
	}
}
